package com.example.demo.controller;

import com.example.demo.common.*;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CheckCodeTools codeTools;
    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();


    @RequestMapping("/reg")
    public AjaxResult reg(UserInfo userInfo) {
        // 进行非空判断
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
            !StringUtils.hasLength(userInfo.getPassword()))
            return AjaxResult.fail(-1,"参数有误","");
        // 调用userService 执行添加方法 并将结果返回
        // 将密码进行加盐加码
        //userInfo.setPassword(PasswordTools.encrypt(userInfo.getPassword()));
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        int result = userService.reg(userInfo);
        return AjaxResult.success(result);
    }

    @RequestMapping("/login")
    public AjaxResult login(String username, String password, HttpServletRequest request){
        // 非空效验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password))
            return AjaxResult.fail(-1,"参数有误","");
        UserInfo userInfo = userService.login(username);
        if(userInfo == null || userInfo.getId() <= 0){
            return AjaxResult.fail(-2,"用户名或者密码错误!","");
        }
//        if(!PasswordTools.decrypt(password, userInfo.getPassword())){
//            return AjaxResult.fail(-2,"用户名或者密码错误!","");
//        }
        if(!passwordEncoder.matches(password, userInfo.getPassword())){
            return AjaxResult.fail(-2,"用户名或者密码错误!","");
        }
        // 将当前成功登录的用户信息存储到session中
        HttpSession session = request.getSession();
        session.setAttribute(ApplicationVariable.SESSION_KEY_USERINFO,userInfo);
        return AjaxResult.success(1);
    }

    /***
     * 退出操作
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        session.removeAttribute(ApplicationVariable.SESSION_KEY_USERINFO);
        return AjaxResult.success(1);
    }


    @RequestMapping("/islogin")
    public AjaxResult isLogin(HttpServletRequest request) {
        if(UserSessionTools.getLoginUser(request) == null){
            return AjaxResult.fail(-1,"参数有误","");
        }
        return AjaxResult.success(1);
    }

    @RequestMapping("/getcode")
    public AjaxResult getCode(HttpServletRequest request) {
        String[] codeArr = codeTools.createImage();
        // 将验证码存储到 session
        HttpSession session = request.getSession();
        session.setAttribute(ApplicationVariable.SESSION_KEY_CHECK_CODE, codeArr[1]);
        return AjaxResult.success("/image/" + codeArr[0]);
    }
}
